<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>微信分组</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            微信分组管理&nbsp;&nbsp;
                            <select id="status" style="layui-input-block" onchange="reloadList()">
                                <option value="1">启用的</option>
                                <option value="0">禁用的</option>
                                <option value="-1">全部</option>
                            </select>&nbsp;&nbsp;
                            <a href="add.html" class="layui-btn layui-btn-sm">添加组</a>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm" onclick="batchAddFriend(1)">一键开启加人</button>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm layui-btn-warm" onclick="batchAddFriend(0)">一键关闭加人</button>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm" onclick="batchFakeAddFriend(1)">一键开启养号</button>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm layui-btn-warm" onclick="batchFakeAddFriend(0)">一键关闭养号</button>
                        </div>
                        <div class="layui-card-body">
                            <table id="LAY-group-manage" lay-filter="LAY-group-manage"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        var reloadList;
        var changeGroup;
        var batchAddFriend;
        var batchFakeAddFriend;
        var exportWeChat;
        layui.use(['form', 'table', 'laytpl', 'element'], function(){
            var $ = layui.$
                ,form = layui.form
                ,layer = layui.layer
                ,table = layui.table
                ,laytpl = layui.laytpl
                ,element = layui.element;

            element.init();

            var status = $('#status').find("option:selected").val();
            table.render({
                elem: '#LAY-group-manage'
//            ,height: 315
                ,url: '/group/list_data' //数据接口
                ,where: {//设定异步数据接口的额外参数
                    status: status
                }
                ,page: true //开启分页
                ,cols: [[ //表头
//                {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                    {field: 'name', title: '名称', fixed: 'left', templet: '#nameTpl'}
                    ,{field: 'version', title: '登录版本', width:140, templet: '#versionTpl'}
                    ,{field: 'wechat_num', title: '微信数量/在线', width:120, templet: '#weChatNumTpl'}
                    ,{field: 'add_friend', title: '加人', width:90, templet: '#addFriendStatusTpl'}
                    ,{field: 'add_friend_auto', title: '自动通过', width:90, templet: '#addFriendAutoStatusTpl'}
                    ,{field: 'add_friend_day_limit', title: '加人频率', width:230, templet: '#addFriendDayLimitTpl'}
                    ,{field: 'fake_add_friend', title: '养号', width:90, templet: '#fakeAddFriendStatusTpl'}
                    ,{field: 'status', title: '状态', width:70, templet: '#statusTpl', unresize: true}
                    ,{field: 'created', title: '添加时间', width:170, sort: true, hide: true}
                    ,{fixed: 'right', align:'center', toolbar: '#barDemo', width:360}
                ]]
                ,title: '计划列表'
                ,toolbar: '#toolbarDemo'
                ,defaultToolbar: ['filter']
            });

            //监听工具条
            table.on('tool(LAY-group-manage)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                if(layEvent === 'detail'){ //查看
                    //do somehing
                } else if(layEvent === 'del'){ //删除
                    batchDeleteByGroupV2(data);
                } else if(layEvent === 'edit'){ //编辑
                    //do something
                } else if(layEvent === 'change'){ //切换组
                    changeGroup(data);
                } else if(layEvent === 'batchLogout'){ //注销
                    batchLogout(data);
                } else if(layEvent === 'batchLogin'){ //登录
                    batchLogin(data);
                } else if(layEvent === 'changePwd'){ //改密
                    changePwd(data);
                } else if(layEvent === 'export'){ //改密
                    exportWeChat(data);
                }
            });

            reloadList = function () {
                var status = $('#status').find("option:selected").val();
                table.reload('LAY-group-manage', {
                    where: { //设定异步数据接口的额外参数，任意设
                        status: status
                    }
                    ,page: {
//                    curr: 1 //重新从第 1 页开始
                    }
                });
            };

            changeGroup = function (data) {
                var board_html = '';
                var getTpl = tpl_change_group.innerHTML;
                laytpl(getTpl).render(data, function(html){
                    board_html = $.trim(html);
                });
                layer.open({
                    title: '将分组内的小号切换到新分组'
                    ,type: 1
                    ,area: '500px'
                    //,skin: 'layui-layer-rim'
                    ,shadeClose: true
                    ,content: board_html
                    ,btn: ['确定', '取消']
                    ,success: function () {
                        form.render();
                    }
                    ,yes: function(index, layero){
                        var oldIdObj = layero.find('input[name=old_group_id]');
                        var oldId = oldIdObj.val();
                        var newIdObj = layero.find('select[name=new_group_id]').find('option:selected');
                        var newId = newIdObj.val();
                        var amountObj = layero.find('input[name=amount]');
                        var amount = amountObj.val();
                        var loginStatusObj = layero.find('input[name=login_status]:checked');
                        var loginStatus = loginStatusObj.val();
//                    console.log('newIdObj:'+newIdObj);
//                    console.log('oldId:'+oldId);
//                    console.log('newId:'+newId);
//                    console.log('amount:'+amount);
//                    console.log('loginStatus:'+loginStatus);
                        if (oldId > 0 && newId>0) {
                            var post = {
                                old_group_id: oldId,
                                new_group_id: newId,
                                amount: amount,
                                login_status: loginStatus
                            };
                            $.ajax({
                                type: "POST",
                                url: "/group/changeGroup",
                                data: post,
                                success:function(response){
                                    var dataObj=$.parseJSON(response);
                                    if (dataObj.code === 0) {
                                        layer.msg(dataObj.msg, {icon: 1});
                                        layer.close(index);
                                        setTimeout(function(){
                                            reloadList();
                                        }, 1000);
                                    } else {
                                        layer.msg(dataObj.msg, {icon: 5});
                                    }
                                },
                                error: function (request, status, error) {
                                    layer.msg('哦噢，网络开小差了', {icon: 5});
                                }
                            });
                        } else {
                            layer.msg('分组ID不存在', {icon: 5});
                            return false;
                        }
                    }
                    ,btn2: function(index, layero){

                    }
                });
            };

            //监听状态操作
            form.on('switch(addFriendStatus)', function(obj){
                var add_friend = 0;
                if (obj.elem.checked) {
                    add_friend = 1;
                }
                var id = this.step;
                var post = {
                    id: id,
                    add_friend: add_friend
                };
                $.ajax({
                    type: "POST",
                    url: "/group/changeAddFriendStatus",
                    data: post,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if(dataObj.code == -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }

                        setTimeout(function(){
                            reloadList();
                        }, 1000);
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            //监听自动通过状态操作
            form.on('switch(addFriendAutoStatus)', function(obj){
                var add_friend_auto = 0;
                if (obj.elem.checked) {
                    add_friend_auto = 1;
                }
                var id = this.step;
                var post = {
                    id: id,
                    add_friend_auto: add_friend_auto
                };
                $.ajax({
                    type: "POST",
                    url: "/group/changeAddFriendAutoStatus",
                    data: post,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if(dataObj.code == -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }

                        setTimeout(function(){
                            reloadList();
                        }, 1000);
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            form.on('switch(fakeAddFriendStatus)', function(obj){
                var fake_add_friend = 0;
                if (obj.elem.checked) {
                    fake_add_friend = 1;
                }
                var id = this.step;
                var post = {
                    id: id,
                    fake_add_friend: fake_add_friend
                };
                $.ajax({
                    type: "POST",
                    url: "/group/changeFakeAddFriendStatus",
                    data: post,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if(dataObj.code == -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }

                        setTimeout(function(){
                            reloadList();
                        }, 1000);
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            var batchLogout = function (data) {
                layer.confirm('确定注销该组微信吗？', function(index){
                    var id = data.id;
                    var post = {
                        group_id: id
                    };
                    $.ajax({
                        type: "POST",
                        url: "/wechat/batchLogout",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            var batchLogin = function (data) {
                layer.confirm('确定登录该组微信吗？(只适合通过账号密码登录)', function(index){
                    var id = data.id;
                    var post = {
                        group_id: id
                    };
                    $.ajax({
                        type: "POST",
                        url: "/wechat/batchLogin",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            var changePwd = function (data) {
                layer.confirm('确定修改该组微信密码吗？', function(index){
                    var id = data.id;
                    var post = {
                        group_id: id
                    };
                    $.ajax({
                        type: "POST",
                        url: "/wechat/changePwdByGroup",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            exportWeChat = function (data) {
                /*layer.confirm('确定导出该组微信吗？', function(index){
                    location.href = "/wechat/export/"+data.id;
                    layer.close(index);
                });
                return false;*/

                var title = '导出微信号';
                var board_html = '';
                var getTpl = tpl_export1.innerHTML;
                var data2 = {};
                laytpl(getTpl).render(data2, function(html){
                    board_html = $.trim(html);
                });
                layer.open({
                    title: title
                    ,type: 1
                    ,area: '400px'
                    //,skin: 'layui-layer-rim'
                    ,shadeClose: true
                    ,content: board_html
                    ,btn: ['确定', '取消']
                    ,success: function(layero, index){
                        form.render();
                    }
                    ,yes: function(index, layero){
                        location.href = "/wechat/export/"+data.id;
                        layer.close(index);
                    }
                    ,btn2: function(index, layero){

                    }
                });
            };

            batchAddFriend = function (status) {
                var warningText = '确定全部'+(status==1 ? '开启' : '关闭')+'加人吗？';
                layer.confirm(warningText, function(index){
                    var post = {
                        status: status
                    };
                    $.ajax({
                        type: "POST",
                        url: "/group/batchAddFriend",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            batchFakeAddFriend = function (status) {
                var warningText = '确定全部'+(status==1 ? '开启' : '关闭')+'养号吗？';
                layer.confirm(warningText, function(index){
                    var post = {
                        status: status
                    };
                    $.ajax({
                        type: "POST",
                        url: "/group/batchFakeAddFriend",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            var batchDeleteByGroup = function (data) {
                layer.confirm('确定删除该组微信吗？删除将在第二天凌晨执行', function(index){
                    var id = data.id;
                    var post = {
                        del: 1,
                        group_id: id,
                        delete_type: 1
                    };
                    $.ajax({
                        type: "POST",
                        url: "/wechat/batchDeleteByGroup",
                        data: post,
                        success:function(response){
                            var dataObj = $.parseJSON(response);
                            if(dataObj.code == 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function(){
                                reloadList();
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(index);
                });
                return false;
            };

            var batchDeleteByGroupV2 = function (data) {
                var board_html = '';
                var getTpl = tpl_batch_delete.innerHTML;
                laytpl(getTpl).render(data, function(html){
                    board_html = $.trim(html);
                });
                layer.open({
                    title: '删除分组和分组内的微信'
                    ,type: 1
                    ,area: '500px'
                    //,skin: 'layui-layer-rim'
                    ,shadeClose: true
                    ,content: board_html
                    ,btn: ['确定', '取消']
                    ,success: function () {
                        form.render();
                    }
                    ,yes: function(index, layero){
                        var groupIdObj = layero.find('input[name=group_id]');
                        var groupId = groupIdObj.val();
                        var deleteTypeObj = layero.find('input[name=delete_type]:checked');
                        var deleteType = deleteTypeObj.val();
//                    console.log('newIdObj:'+newIdObj);
//                    console.log('oldId:'+oldId);
                        if (groupId>0) {
                            var post = {
                                del: 1,
                                group_id: groupId,
                                delete_type: deleteType
                            };
                            $.ajax({
                                type: "POST",
                                url: "/wechat/batchDeleteByGroup",
                                data: post,
                                success:function(response){
                                    var dataObj=$.parseJSON(response);
                                    if (dataObj.code === 0) {
                                        layer.msg(dataObj.msg, {icon: 1});
                                        layer.close(index);
                                        setTimeout(function(){
                                            reloadList();
                                        }, 1000);
                                    } else {
                                        layer.msg(dataObj.msg, {icon: 5});
                                    }
                                },
                                error: function (request, status, error) {
                                    layer.msg('哦噢，网络开小差了', {icon: 5});
                                }
                            });
                        } else {
                            layer.msg('分组ID不存在', {icon: 5});
                            return false;
                        }
                    }
                    ,btn2: function(index, layero){

                    }
                });
            };
        });

    </script>
    <script type="text/html" id="barDemo">
        {{# if(d.request_delete == 1){ }}
        <span style="color: red;">第二天凌晨删除</span>
        {{# } else if(d.request_delete == 2){ }}
        <span style="color: red;">即将删除</span>
        {{# } else { }}
        <button type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
        <a class="layui-btn layui-btn-xs" lay-event="edit" href="/v1/group/edit/{{d.id}}" target="_self">编辑</a>
        <button type="button" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="change">切换</button>
        <button type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="batchLogout">注销</button>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="batchLogin">登录</button>
        <button type="button" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="changePwd">改密</button>
        <button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="export">导出</button>
        {{# } }}
    </script>
    <script type="text/html" id="statusTpl">
        {{# if(d.status == 0){ }}
        <span style="color: red;">禁用</span>
        {{# } else if(d.status == 1){ }}
        <span style="color: green;">启用</span>
        {{# } }}
    </script>
    <script type="text/html" id="addFriendStatusTpl">
        <input type="checkbox"  name="add_friend" step="{{d.id}}" value="{{d.add_friend}}" lay-skin="switch" lay-text="开启|停止" lay-filter="addFriendStatus" {{ d.add_friend == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="addFriendAutoStatusTpl">
        <input type="checkbox"  name="add_friend_auto" step="{{d.id}}" value="{{d.add_friend_auto}}" lay-skin="switch" lay-text="自动|手动" lay-filter="addFriendAutoStatus" {{ d.add_friend_auto == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="fakeAddFriendStatusTpl">
        <input type="checkbox"  name="fake_add_friend" step="{{d.id}}" value="{{d.fake_add_friend}}" lay-skin="switch" lay-text="开启|停止" lay-filter="fakeAddFriendStatus" {{ d.fake_add_friend == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="weChatNumTpl">
        <a class="layui-btn layui-btn-xs" lay-event="edit" href="/v1/wechat/index/{{d.id}}" target="_self">{{d.wechat_num}} / {{d.wechat_login_num}}</a>
    </script>
    <script type="text/html" id="addFriendDayLimitTpl">
        {{d.add_friend_day_limit}}轮， 每轮{{d.add_friend_per_round}}次，间隔{{d.upload_friend_interval}}分钟
    </script>
    <script id="tpl_change_group" type="text/html">
        <form class="layui-form" action="">
            <div class="layui-form-item layui-form-text">
                <input type="hidden" name="old_group_id" value="{{ d.id }}">
                <div class="layui-form-item">
                    <label class="layui-form-label">微信组</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-mid">{{ d.name }}</label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">新微信分组</label>
                    <div class="layui-input-inline">
                        <select name="new_group_id" class="layui-input">
                            <option value="0">选择分组</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="login_status" value="2" title="全部" checked>
                        <input type="radio" name="login_status" value="1" title="已登录">
                        <input type="radio" name="login_status" value="0" title="未登录">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">数量</label>
                    <div class="layui-input-inline">
                        <input type="text" name="amount" autocomplete="off" class="layui-input" value=""  placeholder="不填则切换全部小号">
                    </div>
                </div>
            </div>
        </form>
    </script>
    <script id="tpl_export1" type="text/html">
        <div style="padding: 10px;">
            <div class="layui-form-item">
                <div class="layui-form-item">
                    <label class="layui-form-mid">确定导出该组微信吗？</label>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-mid layui-word-aux">提示：设置二级密码后，导出时需要验证二级密码。<a href="../account/setProtectPassword.html" target="_blank">去设置</a></label>
                </div>
            </div>
        </div>
    </script>
    <script id="tpl_export2" type="text/html">
        <form id="exportForm" class="layui-form" action="/v1/wechat/export/{{ d.id }}" method="post" target="_blank">
            <div class="layui-form-item layui-form-text">
                <div class="layui-form-item">
                    <label class="layui-form-label">二级密码</label>
                    <div class="layui-input-inline">
                        <input type="password" autocomplete="off" placeholder="请输入二级密码" class="layui-input" value="" id="protect_password">
                        <input type="hidden" name="protect_password" autocomplete="off" value="" id="protect_password_md5">
                    </div>
                </div>
            </div>
        </form>
    </script>
    <script type="text/html" id="nameTpl">
        {{d.name}}
        {{# if(d.test == 1){ }}
        <span style="color: green;float: right;">测试组</span>
        {{# } }}
    </script>
    <script id="tpl_batch_delete" type="text/html">
        <form class="layui-form" action="">
            <div class="layui-form-item layui-form-text">
                <input type="hidden" name="group_id" value="{{ d.id }}">
                <div class="layui-form-item">
                    <label class="layui-form-label">微信组</label>
                    <div class="layui-input-inline">
                        <label class="layui-form-mid">{{ d.name }}</label>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">温馨提示</label>
                    <div class="layui-input-block">
                        <label class="layui-form-mid alert-warning">同时删除组内的所有微信，确定要删除吗？</label>
                    </div>
                </div>
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">请求删除</label>
                    <div class="layui-input-block">
                        <input type="radio" name="delete_type" value="1" title="第二天凌晨删除" checked>
                        <input type="radio" name="delete_type" value="2" title="10分钟后删除">
                    </div>
                </div>
            </div>
        </form>
    </script>
    <script type="text/html" id="versionTpl">
        {{ d.version || '默认' }}
    </script>
</div>
</body>
</html>
